<template>
  <div class="app-container">
    <el-form ref="queryForm" :model="queryParams" size="mini" inline style="white-space: nowrap; overflow-x: auto" :class="{ disabled: disableAction }">
      <el-form-item prop="chkCode">
        <el-input v-model.trim="queryParams.chkCode" placeholder="请输入编码" clearable style="width: 130px" />
      </el-form-item>
      <el-form-item prop="chkName">
        <el-input v-model.trim="queryParams.chkName" placeholder="请输入名称" clearable style="width: 130px" />
      </el-form-item>
      <el-form-item>
        <el-button size="mini" icon="el-icon-refresh" @click="resetQuery">重置</el-button>
        <el-button size="mini" :icon="isExpand ? 'el-icon-arrow-down' : 'el-icon-arrow-right'" @click="handleExpandDebounce">全部{{ isExpand ? '折叠' : '展开' }}</el-button>
        <!--<el-button size="mini" type="primary" icon="el-icon-plus" @click="$refs.EditItem.open({}, { treeData: list })" v-if="showBtns.add">新增</el-button>-->
      </el-form-item>
    </el-form>

    <el-table class="g-table-tree-expand-align" ref="table" v-loading="listLoading" :data="filteredList" size="small" row-key="chkCode" :default-expand-all="isExpand" height="calc(100vh - 150px)">
      <el-table-column label="编码" prop="chkCode" show-overflow-tooltip min-width="200" class-name="expand-column" />
      <el-table-column label="名称" prop="chkName" show-overflow-tooltip min-width="150" />
      <el-table-column label="权重" align="center" show-overflow-tooltip min-width="400">
        <!--<span slot-scope="{ row }">{{ row.weight }}</span>-->
        <template slot-scope="{ row }">
          <div style="white-space: normal">
            <el-tag  type="warning" style="margin:  0 10px 10px 0">经营单位集群： 10%</el-tag>
            <el-tag  type="warning" style="margin:  0 10px 10px 0">经营单位事业部： 20%</el-tag>
            <el-tag  type="warning" style="margin:  0 10px 10px 0">经营单位事业部工程部： 10%</el-tag>
            <el-tag  type="warning" style="margin:  0 10px 10px 0">经营单位项目部：10%</el-tag>
            <el-tag  type="warning" style="margin:  0 10px 10px 0">职能部门集群： 10%</el-tag>
            <el-tag  type="warning" style="margin:  0 10px 10px 0">职能部门事业部： 20%</el-tag>
            <el-tag  type="warning" style="margin:  0 10px 10px 0">分管领导集群： 10%</el-tag>
            <el-tag  type="warning" style="margin:  0 10px 10px 0">分管领导事业部： 20%</el-tag>
          </div>
        </template>
      </el-table-column>
      <!--<el-table-column label="计算方式" align="center" show-overflow-tooltip min-width="100">-->
        <!--<span slot-scope="{ row, $index }">{{ $index % 2 === 1 ? '项目级平均' : '检查次数平均' }}</span>-->
      <!--</el-table-column>-->
      <!--<el-table-column label="考核对象" align="center" show-overflow-tooltip min-width="100">-->
        <!--<span slot-scope="{ row, $index }">{{ $index % 2 === 1 ? '经营单位' : '分管领导' }}</span>-->
      <!--</el-table-column>-->
      <el-table-column label="扩展" align="center" show-overflow-tooltip min-width="200">
        <span slot-scope="{ row }">{{ row.extendField }}</span>
      </el-table-column>
      <el-table-column label="操作" fixed="right" align="center" width="300">
        <template slot-scope="{ row }">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="$refs.EditItem.open(row, { treeData: list })" v-if="showBtns.edit">修改</el-button>
          <!--<el-button size="mini" type="text" icon="el-icon-delete" :loading="row.removeLoading" @click="handleDelete(row)" v-if="showBtns.remove">删除</el-button>-->
          <!--<el-button size="mini" type="text" icon="el-icon-plus" @click="$refs.EditItem.open({ parentCode: row.chkCode }, { treeData: list })" v-if="showBtns.add">新增</el-button>-->
          <el-button size="mini" type="text" icon="el-icon-setting" @click="$refs.EditCfg.open(row)" v-if="showBtns.config">配置</el-button>
        </template>
      </el-table-column>
    </el-table>

    <EditItem ref="EditItem" v-model="showEditItem" @ok="getList" />
    <EditCfg ref="EditCfg" v-model="showEditCfg" @ok="getList" />
  </div>
</template>
<script>
// 工程质量-评估-检查项
import { debounce, flatList2tree, isLikeOrEqual } from '@/utils'
import { qcChkitemconfigList, qcChkitemconfigDelete } from '@/api/checkScore/aoZheChkItem'
import EditItem from './EditAZChkitem'
import EditCfg from './EditAZChkitemCfg'
import auth from '@/plugins/auth'

export default {
  name: 'AoZheChkItem',
  components: { EditItem, EditCfg },
  filters: {
    multivalueFilter(val, value2label = {}) {
      const str = val || ''
      return str
        .split(',')
        .map(i => value2label[i])
        .filter(i => i)
        .join('、')
    }
  },
  data() {
    return {
      isExpand: true,
      listLoading: false,
      list: [],
      filteredList: [],
      queryParams: {
        chkCode: '',
        chkName: ''
      },
      showEditItem: false,
      showEditCfg: false
    }
  },
  computed: {
    disableAction() {
      return this.listLoading
    },
    showBtns() {
      // 操作按钮权限
      const edit = auth.hasPermi(`checkScore:aoZheChkItem:edit`)
      const add = auth.hasPermi(`checkScore:aoZheChkItem:add`)
      const remove = auth.hasPermi(`checkScore:aoZheChkItem:remove`)
      const config = auth.hasPermi(`checkScore:aoZheChkItem:config`)
      return { edit, add, remove, config }
    }
  },
  watch: {
    queryParams: {
      deep: true,
      handler() {
        this.handleQueryDebounce()
      }
    }
  },
  created() {
    this.handleQueryDebounce = debounce(this.handleQuery)
    this.handleExpandDebounce = debounce(this.handleExpand)
    this.getList()
  },
  methods: {
    // 递归切换展开状态
    setChildren(children, expanded) {
      children.map(row => {
        this.$refs.table.toggleRowExpansion(row, expanded)
        if (row.children) this.setChildren(row.children, expanded)
      })
    },
    // 展开/折叠
    handleExpand() {
      this.isExpand = !this.isExpand
      this.setChildren(this.filteredList, this.isExpand)
    },
    async getList() {
      const opt = { ...this.queryParams }
      this.listLoading = true
      const res = await qcChkitemconfigList(opt).catch(() => false)
      this.listLoading = false
      if (res === false) return

      let data = res.data || []
      data.map(i => {
        i.isEnableLoading = false
        i.removeLoading = false
      })
      data.sort((obj1, obj2) => {
        return obj1.chkCode.localeCompare(obj2.chkCode)
      })
      data = flatList2tree(data, { id: 'chkCode', parent: 'parentCode' })
      this.list = data
      this.handleQuery()
    },
    handleQuery() {
      // 本地树形搜索
      const { chkCode, chkName } = this.queryParams
      const opts = { like: { chkCode, chkName } }
      const list = JSON.parse(JSON.stringify(this.list))
      const recursion = list =>
        list.filter(item => {
          if (item.children?.length) item.children = recursion(item.children)
          return isLikeOrEqual(item, opts) || item.children?.length
        })
      const data = recursion(list)
      this.filteredList = data
      this.isExpand = true
    },
    resetQuery() {
      this.resetForm('queryForm')
      this.getList()
    },
    async handleDelete(item) {
      if (item.removeLoading) return
      const res1 = await this.$confirm(`确认删除"${item.chkName}"?`, '警告', { type: 'warning' }).catch(() => false)
      if (res1 !== 'confirm') return

      item.removeLoading = true
      const res = await qcChkitemconfigDelete({ chkitemId: item.id }).catch(() => false)
      item.removeLoading = false
      if (res === false) return

      this.msgSuccess('删除成功')
      this.getList()
    }
  }
}
</script>
<style lang="scss" scoped>
.version-code {
  position: absolute;
  font-size: 14px;
  right: 12px;
  top: 12px;
  color: #909399;
}
</style>
